<?php

/** 
 * BaseModel for all other database access models
 * @author Roshan
 * @version 1.0;
 * 
 */
abstract class baseModel {
	public $con;
	
	function __construct() {
		$this->con = DB::newInstance ();
		
	}
	
	function getTableName() {
	}
	
	function find($args = array()) {
		if (count ( $args ) < 1) {
			return FALSE;
		}
		$columns = array ();
		$new_args = array ();
		foreach ( $args as $key => $value ) {
			$columns [] = $key . '=:' . $key;
			$new_args [':' . $key] = $value;
		}
		$where = implode ( ' AND ', $columns );
		
		$sql = sprintf ( 'SELECT * FROM %s WHERE ' . $where, $this->getTableName () );
		//echo $sql;
		return $this->con->query ( $sql, $new_args );
	
	}
	
        function query($sql,$args=array()){
            return $this->con->query ( $sql, $args);
        }
	function exists($args){
		if (count ( $args ) < 1) {
			return FALSE;
		}
		$columns = array ();
		$new_args = array ();
		foreach ( $args as $key => $value ) {
			$columns [] = $key . '=:' . $key;
			$new_args [':' . $key] = $value;
		}
		$where = implode ( ' AND ', $columns );
                unset($new_args);// unset argument array
		
		$sql = sprintf ( 'SELECT * FROM %s WHERE ' . $where, $this->getTableName () );
		
		$result=$this->con->query ( $sql, $new_args );
		if(count($result)>0){
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function delete($args) {
		if(!is_array($args)){
			return FALSE;
		}
		if (count ( $args ) < 1) {
			return FALSE;
		}
		$columns = array ();
		$new_args = array ();
		foreach ( $args as $key => $value ) {
			$columns [] = $key . '=:' . $key;
			$new_args [':' . $key] = $value;
		}
		$where = implode ( ' AND ', $columns );
		
		$sql = sprintf ( 'DELETE FROM %s WHERE ' . $where, $this->getTableName () );
		//echo $sql;
		

		return $this->con->delete ( $sql, $new_args );
	}
	
	function update($args = array(), $fields = array()) {
		if (count ( $args ) < 1) {
			return FALSE;
		}
		if (count ( $fields ) < 1) {
			return FALSE;
		}
		
		$columns = array ();
		$new_args = array ();
		foreach ( $args as $key => $value ) {
			$columns [] = $key . '=:' . $key;
			$new_args [':' . $key] = $value;
		}
		
		$where = implode ( ' AND ', $columns );
		$update = array ();
		foreach ( $fields as $key => $value ) {
			$update [] = $key . '=' . $value;
		}
		
		$fields = implode ( ' AND ', $update );
		
		$sql = sprintf ( 'UPDATE %s SET %s WHERE ' . $where, $this->getTableName (), $fields );
		//echo $sql;
	
		return $this->con->update ( $sql, $new_args );
	}
	function insert($args) {
		if (count ( $args ) < 1) {
			return FALSE;
		}
		$columns = array ();
		$new_args = array ();
		$values=array();
		foreach ( $args as $key => $value ) {
			$columns [] = $key;
			$values[]=':'.$key;
			$new_args[':'.$key]=$value;
		}
		
		
		$sql = sprintf ( 'INSERT INTO  %s (%s) values (%s)', $this->getTableName (),implode(',',$columns),implode(',',$values) );
		//echo $sql;
		
		return $this->con->insert ( $sql, $new_args );
	}
	function last_insert_id(){
		return $this->con->last_insert_id();
	}
        
        function beginTrans(){
            $this->con->beginTrans();
        }
        
        function commit(){
            $this->con->commit();
        }
        
        function rollback(){
            $this->con->rollback();
        }
	

}

?>